<template>
  <div class="my-tag" @dblclick="shuangji">
    <!-- keydown 键盘事件  -->
    <input
      @keydown.enter="nojujiao"
      class="input"
      type="text"
      placeholder="输入标签"
      v-if="aa"
      v-focus
      @blur="nojujiao"
      v-model="kuang"
    />
    <!-- blur 模糊 -->
    <div class="text" v-else> {{value}} </div>
  </div>
</template>

<script>
export default {
  // 离谱（没搞懂为啥传value生效）反正能用就行，以后也这么写
  // value 值
  props: ['value'],
  data() {
    return {
      aa: false,
      kuang: '',
    };
  },
  methods: {
    // 双击
    shuangji() {
      this.aa = true;
      this.kuang = this.value
    },
    // 失焦隐藏
    nojujiao() {
      this.aa = false;
      // 更新父级内容
      this.$emit('input',this.kuang)
    },
  },
  // directives 指令
  directives: {
    // 说是自动获取焦点
    focus: {
      inserted(el) {
        el.focus();
      },
    },
  },
};
</script>

<style lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
